package com.zjmcollege.hotpot.service.impl;

import com.zjmcollege.hotpot.component.FileHandler;
import com.zjmcollege.hotpot.entity.Article;
import com.zjmcollege.hotpot.enums.FileType;
import com.zjmcollege.hotpot.model.ArticleModel;
import com.zjmcollege.hotpot.model.UploadResultModel;
import com.zjmcollege.hotpot.repository.ArticleRepository;
import com.zjmcollege.hotpot.search.model.SearchResultModel;
import com.zjmcollege.hotpot.search.repository.SearchRepository;
import com.zjmcollege.hotpot.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

import static com.zjmcollege.hotpot.enums.Constants.MD;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-11-07 21:10
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private FileHandler fileHandler;
    @Autowired
    private ArticleRepository repository;
    @Autowired
    private SearchRepository searchRepository;

    @Transactional
    @Override
    public Article save(ArticleModel articleModel) {
        if (articleModel.getDocType() == MD) {
            UploadResultModel resultMd = fileHandler.
                    upload(articleModel.getBody(), FileType.MD);

            articleModel.setBody(resultMd.getUrl());
        }
        Article article = new Article();
        BeanUtils.copyProperties(articleModel, article);
        article.setAuthor("编程的猫");
        Article save = repository.save(article);

        if (articleModel.getId() == null) {
            // 像搜索引擎中添加博客数据
            SearchResultModel searchModel = new SearchResultModel();
            searchModel.setAuthor(save.getAuthor());
            searchModel.setTitle(save.getTitle());
            searchModel.setBrief(save.getBrief());
            searchModel.setUrl("/blog/" + save.getId());
            searchModel.setPostedTime(save.getCreatedTime());
            searchRepository.save(searchModel);
        }

        return save;
    }

    @Override
    public Page<Article> query(String key, int page, int pageSize) {

        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);  //分页信息

        return repository.findByKey(key, pageable);
    }

    @Override
    public Article findById(Long id) {
        Optional<Article> article = repository.findById(id);
        Article art = article.get();

        if (art.getDocType() == MD) {
            // 如果上传的文档类型是 md文档，则将文档替换成 md 文档的具体内容
            art.setBody(fileHandler.
                    getContent(art.getBody(), FileType.MD));
        }
        return art;
    }

    /**
     * 分页查询所有的博客
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> query(int page, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);  //分页信息

        return repository.findAll(pageable);
    }
}
